package com.cbh.amadeus.server.provider.tool.impl;

import com.cbh.amadeus.server.domain.AckResultModel;
import com.cbh.amadeus.server.provider.tool.AckResultModelTool;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * @author ChenBiHui
 * @date 2025/4/30
 */
@Slf4j
public class DefaultAckResultModelTool implements AckResultModelTool {

    private final AtomicLong sequence = new AtomicLong(0);
    private final Map<String, AckResultModel> record = new ConcurrentHashMap<>(64);


    @Override
    public String sequence() {
        return String.valueOf(sequence.incrementAndGet());
    }

    @Override
    public void finish(AckResultModel model) {
        record.putIfAbsent(model.getId(), model);
    }

    @Override
    public List<AckResultModel> match(String tenant, String topic, String messageId) {
        return record.values()
                .stream()
                .filter(item -> tenant.equals(item.getTenant()))
                .filter(item -> topic.equals(item.getTopic()))
                .filter(item -> messageId.equals(item.getMessageId()))
                .collect(Collectors.toList());
    }
}
